package com.fh.db.mysql;

import com.fh.db.DbFunction;
import com.fh.db.DbPackage;
import com.fh.db.DbProcedure;
import com.fh.db.DbSource;
import com.fh.db.DbTable;
import com.fh.db.DbView;
import com.fh.db.SqlRunner;
import com.fh.db.base.BaseDbSchema;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class MysqlDbSchema extends BaseDbSchema {
    @Override
    public List<DbTable> getTables() {
        return (List<DbTable>) SqlRunner.preparedSqlResult(dbSource, dbSource.getSqlSet().tablePreSql(), new String[]{"test"},
                resultSet -> {
                    List<DbTable> result = new ArrayList<>();
                    try {
                        while (resultSet.next()) {
                            MysqlDbTable mysqlDbTable = new MysqlDbTable();
                            mysqlDbTable.setDbSource(dbSource);
                            mysqlDbTable.setTableName(resultSet.getString("table_name"));
                            mysqlDbTable.setOwner(resultSet.getString("table_schema"));
                            mysqlDbTable.setTablespaceName("");
                            mysqlDbTable.setTableComments(resultSet.getString("table_comment"));
                            BigDecimal numRows = resultSet.getBigDecimal("table_rows");
                            mysqlDbTable.setRowNum(numRows == null ? null : numRows.intValue());
                            mysqlDbTable.setCreated(resultSet.getTimestamp("create_time"));
                            mysqlDbTable.setLastDdlTime(resultSet.getTimestamp("update_time"));
                            result.add(mysqlDbTable);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return result;
                });

    }

    @Override
    public List<DbView> getViews() {
        return null;
    }

    @Override
    public List<DbFunction> getFunctions() {
        return null;
    }

    @Override
    public List<DbProcedure> getProcedures() {
        return null;
    }

    @Override
    public List<DbPackage> getPackages() {
        return null;
    }

    @Override
    public DbSource getDbSource() {
        return this.dbSource;
    }

    public void setDbSource(DbSource dbSource) {
        this.dbSource = dbSource;
    }
}
